import jinja2, os, webapp2, urllib, cgi
from datatypes import *
import sessions

jinja_environment = jinja2.Environment(
    loader=jinja2.FileSystemLoader(os.path.dirname(__file__)))

def log(msg):
    l = Log(parent=LogPathGen())
    l.message = msg
    l.put()
    
class viewLog(webapp2.RequestHandler):
    def get(self):
        if sessions.adminOrGtfo(self): return
        logs = Log.all().ancestor(LogPathGen()).order('timeMade')
        self.response.out.write('<table>')
        for logEntry in logs:
            self.response.out.write('<tr>')
            self.response.out.write('<td>{0}</td><td>{1}</td>'.format(
                cgi.escape(logEntry.message), logEntry.timeMade))
            self.response.out.write('</tr>')
        self.response.out.write('</table>')

app = webapp2.WSGIApplication([('/log/view', viewLog)
                               ],
                              debug=True)
